import { delay, isNumber } from '@pmun/utils'
import { sample, times } from 'lodash-es'
import debounce from '@/utils/debounce'
import throttle from '@/utils/throttle'

/** 文件预览 */
export function previewBaseUrl(url) {
  if (!url) return ''
  const isAbsoluteURL = url.startsWith('http://') || url.startsWith('https://')
  const baseUrl = import.meta.env.VITE_APP_API_BASE_URL
  return isAbsoluteURL ? url : `${baseUrl}/file/previewImg${url}`
}

/** 文件下载 */
export function downloadBaseUrl(url) {
  if (!url) return ''
  const baseUrl = import.meta.env.VITE_APP_API_BASE_URL
  return `${baseUrl}/file/api/external/download?fileUrl=${url}`
}

export const useGoBackHome = () => uni.reLaunch({ url: '/pages/index/index' })

export const useGoBack = (delta = 1) => uni.navigateBack({ delta })

export const useGoLogin = (redirect?: string) => uni.navigateTo({ url: redirect ? '/pages/login/index' : `/pages/login/index?redirect=${redirect}` })

/**
 * 防抖
 * @param func — The function to debounce.
 * @param wait — The number of milliseconds to throttle invocations to.
 * @returns
 */
export const $debounce = (func, wait = 500) => debounce(func, wait) as any

/**
 * 节流
 * @param func — The function to throttle.
 * @param wait — The number of milliseconds to throttle invocations to.
 * @returns
 */
export const $throttle = (func, wait = 500) => throttle(func, wait) as any

/**
 * 延迟指定的毫秒数
 * @param wait — The number of milliseconds to throttle invocations to.
 * @returns
 */
export const sleep = (wait = 50) => delay(wait)

/**
 * 字符串中间星号
 * @returns 字符串
 */
export function stringStar(value = '默认内容', characters = '****', start = 3, end = 4) {
  const reg = new RegExp(`^(.{${start}}).*(.{${end}})$`)
  if (value) {
    return value.replace(reg, `$1${characters}$2`)
  }
  return ''
}

/**
 * @description 用于获取用户传递值的px值  如果用户传递了"xxpx"或者"xxrpx"，取出其数值部分，如果是"xxxrpx"还需要用过uni.upx2px进行转换
 * @param {number|string} value 用户传递值的px值
 * @param {boolean} unit
 * @returns
 */
export function rpxToPx<T extends boolean = false>(value, unit?: T): T extends true ? string : number {
  if (isNumber(value)) {
    return (unit ? `${value}px` : value) as T extends true ? string : number
  }
  // 如果带有rpx，先取出其数值部分，再转为px值
  if (/(?:rpx|upx)$/.test(value)) {
    const pxValue = uni.upx2px(Number.parseInt(value))
    return (unit ? `${pxValue}px` : pxValue) as T extends true ? string : number
  }
  const numValue = Number.parseInt(value)
  return (unit ? `${numValue}px` : numValue) as T extends true ? string : number
}

/**
 * @description 日期的月或日补零操作
 * @param {string} value 需要补零的值
 */
export function padZero(value) {
  return `00${value}`.slice(-2)
}

/** 生成随机码 */
export function generateId(options?: any) {
  const {
    prefix = '',
    length = 8,
    includeNumbers = true,
    includeLetters = true,
    includeTimestamp = false,
  } = options
  let chars = ''
  if (includeLetters) chars += 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
  if (includeNumbers) chars += '0123456789'
  const randomPart = times(length, () => sample(chars)).join('')
  const timestampPart = includeTimestamp ? `_${Date.now()}` : ''
  return `${prefix}${randomPart}${timestampPart}`
}

// { "type": "feat", "section": "✨ Features | 新功能" },
// { "type": "fix", "section": "🐛 Bug Fixes | Bug 修复" },
// { "type": "chore", "section": "🎫 Chores | 其他更新" },
// { "type": "docs", "section": "📝 Documentation | 文档" },
// { "type": "style", "section": "💄 Styles | 风格" },
// { "type": "refactor", "section": "♻ Code Refactoring | 代码重构" },
// { "type": "perf", "section": "⚡ Performance Improvements | 性能优化" },
// { "type": "test", "section": "✅ Tests | 测试" },
// { "type": "revert", "section": "⏪ Reverts | 回退" },
// { "type": "build", "section": "👷‍ Build System | 构建" },
// { "type": "ci", "section": "🔧 Continuous Integration | CI 配置" },
// { "type": "config", "section": "🔨 CONFIG | 配置" }
